package cn.janescott.framework.core.utils;

import org.slf4j.Logger;

import java.util.Optional;
import java.util.function.Supplier;

/**
 * 对外服务基类
 *
 * @author scott
 * @date 2019/1/21
 */
public final class LoggerUtils {

    /**
     * debug日志
     * <p>
     * 1、
     * logger.debug(xxx);
     * <p>
     * 2、
     * if (logger.isDebugEnabled) {
     * logger.debug(xxx);
     * }
     * <p>
     * 第二种性能高于第一种
     *
     * @param logger
     * @param supplier
     */
    public static void debug(Logger logger, Supplier<Object> supplier) {
        if (logger.isDebugEnabled()) {
            logger.debug(Optional.ofNullable(supplier.get()).orElse("null").toString());
        }
    }

    /**
     * error日志
     *
     * @param logger
     * @param supplier
     */
    public static void error(Logger logger, Supplier<Object> supplier) {
        if (logger.isErrorEnabled()) {
            logger.error(Optional.ofNullable(supplier.get()).orElse("null").toString());
        }
    }

    /**
     * warn日志
     *
     * @param logger
     * @param supplier
     */
    public static void warn(Logger logger, Supplier<Object> supplier) {
        if (logger.isWarnEnabled()) {
            logger.warn(Optional.ofNullable(supplier.get()).orElse("null").toString());
        }
    }

    /**
     * info 日志
     *
     * @param logger
     * @param supplier
     */
    public static void info(Logger logger, Supplier<Object> supplier) {
        if (logger.isInfoEnabled()) {
            logger.info(Optional.ofNullable(supplier.get()).orElse("null").toString());
        }
    }

}
